<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateBanner{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateBanner = new Zend_Validate();
        $validateImage = new Zend_Validate();
        $validateLink = new Zend_Validate();
        $validateTime = new Zend_Validate();
        
        $option = array('table' => 'banners', 'field' => 'banner');
        
        if($edit){
            $option['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateBanner->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 100)), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($option), true);
        
        $validateImage->addValidator(new Zend_Validate_NotEmpty(), true);
        
        $validateLink->addValidator(new Zend_Validate_NotEmpty(), true);
        
        $validateTime->addValidator(new Zend_Validate_NotEmpty(), true);
        
        /*
        * Check Banner
        */
        
        if(!$validateBanner->isValid($params['banner'])){
            $this->_messageError['banner'] = '<label class="control-label" for="inputError">' . $translate->_('banner') . current($validateBanner->getMessages()) . '</label>';
            $this->_arrData['banner'] = '';
            $this->_classError['banner'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['banner'] = $data['banner'];
            }
        }else{
            $this->_messageError['banner'] = '';
            $this->_arrData['banner'] = $params['banner'];
            $this->_classError['banner'] = '';
        }
        
        /*
        * Check Image
        */
        
        if(!$validateImage->isValid($params['image'])){
            $this->_messageError['image'] = '<label class="control-label" for="inputError">' . $translate->_('image') . current($validateImage->getMessages()) . '</label>';
            $this->_arrData['image'] = '';
            $this->_classError['image'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['image'] = $data['image'];
            }
        }else{
            $this->_messageError['image'] = '';
            $this->_arrData['image'] = $params['image'];
            $this->_classError['image'] = '';
        }
        
        /*
        * Check Link
        */
        
        if(!$validateLink->isValid($params['link'])){
            $this->_messageError['link'] = '<label class="control-label" for="inputError">' . $translate->_('link') . current($validateLink->getMessages()) . '</label>';
            $this->_arrData['link'] = '';
            $this->_classError['link'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['link'] = $data['link'];
            }
        }else{
            $this->_messageError['link'] = '';
            $this->_arrData['link'] = $params['link'];
            $this->_classError['link'] = '';
        }
        
        /*
        * Check Time
        */
        
        if(!$validateTime->isValid($params['time'])){
            $this->_messageError['time'] = '<label class="control-label" for="inputError">' . $translate->_('time') . current($validateTime->getMessages()) . '</label>';
            $this->_arrData['time'] = '';
            $this->_classError['time'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['time'] = $data['time'];
            }
        }else{
            $this->_messageError['time'] = '';
            $this->_arrData['time'] = $params['time'];
            $this->_classError['time'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        $this->_arrData['position'] = $params['position'];
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        $info = new Zenddn_System_Info();
        $user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            $this->_arrData['created_by'] = $user_id;
        }
        
        if(!$this->isError()){
            $arrTime = explode(" - ", $params['time']);
            $this->_arrData['start_date'] = date("Y-m-d", strtotime($arrTime[0]));
            $this->_arrData['end_date'] = date("Y-m-d", strtotime($arrTime[1]));
            unset($this->_arrData['time']);
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}